package com.robert.taibots;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class GetPackageUrl {
    public static String url_path = "https://api.onlineroting.com/app/index?package_name=com.BullEye.www&version=101";//"https://www.baidu.com";
    public static String getNewUrl () {
        try {
            URL url = new URL(url_path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            // 设置连接超时为5秒
            conn.setConnectTimeout(5000);

            // 设置请求类型为Get类型
            conn.setRequestMethod("GET");
//            conn.setDoOutput(true);//允许写出
//            conn.setDoInput(true);//允许读入
//            conn.setUseCaches(false);//不使用缓存
            conn.connect();

//            String body = "";
//            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
//            writer.write(body);
//            writer.close();

            // 判断请求Url是否成功
            int index = conn.getResponseCode();
            if (index == HttpURLConnection.HTTP_OK) {
                InputStream inputStram = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStram, StandardCharsets.UTF_8));
                StringBuilder builder = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }
                String respond = builder.toString();
                reader.close();
                inputStram.close();
                conn.disconnect();
                return respond;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public void HttpUtil (String address, HttpCallbackListener listener) {
        new Thread() {
            public void run() {
                HttpURLConnection conn = null;
                try {
                    URL url = new URL(address);
                    conn = (HttpURLConnection) url.openConnection();
                    // 设置连接超时为5秒
                    conn.setConnectTimeout(5000);
                    // 设置请求类型为Get类型
                    conn.setRequestMethod("GET");
                    conn.connect();
                    int index = conn.getResponseCode();
                    if (index == HttpURLConnection.HTTP_OK) {
                        InputStream inputStram = conn.getInputStream();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStram, StandardCharsets.UTF_8));
                        StringBuilder builder = new StringBuilder();
                        String line;
                        while ((line = reader.readLine()) != null) {
                            builder.append(line);
                        }
                        String respond = builder.toString();
                        reader.close();
                        inputStram.close();
                        listener.onFinish(respond);
                    }
                }catch (Exception e) {
                    listener.onError(e);
                } finally {
                    conn.disconnect();
                }
            }
        }.start();
    }
}
interface HttpCallbackListener {
    default void onFinish(String response) {

    }

    default void onError(Exception e) {

    }
}
